#!/usr/bin/perl
# ups main page

require './ups-lib.pl';



#&header($text{'index_title'},"",undef,1,1,0,undef,"<meta http-equiv=refresh content=200");
&header($text{'index_title'},"","sample",1,1,0);


my $upsdd = `ps -A |grep upsd`;
my $conf = &get_ups_conf();

if(!$upsdd)
{
	&nas_syslog("UPS","naslog_info",19000,"the ups daemon is not running!");
#	print "<hr>upsd is off<hr>";
	my $drv = `ps -A |grep $conf->{'driver'}`;
#	print "<hr>",$drv,"<hr>";
	if($drv)
	{
		&nas_syslog("UPS","naslog_warning",19001,"wrong driver or com port selected!");
		$drv =~ /(\d+) .*/;
		`kill -9 $1`;
		`upsmon -c stop 2>&1`;
	}
	$drv = `ps -A |grep $conf->{'driver'}`;
#	print "<hr>",$drv,"<hr>";
	if($drv)
	{
		&nas_syslog("UPS","naslog_warning",19001,"wrong driver or com port selected!");
		$drv =~ /(\d+) .*/;
		`kill -9 $1`;
		`upsmon -c stop 2>&1`;
	}
}
else
{
#	print "<hr>upsd is on<hr>";
	&nas_syslog("UPS","naslog_info",19002,"the ups daemon is running!");
	my $state = `upsc localhost`;
#	print "<hr>upsc result:",$state,"<hr>";
	if($state =~ /Unable/ or $state =~ /No/)
	{
		&nas_syslog("UPS","naslog_warning",19003,"Can't connect to ups daemon!");
#		print "<hr>upsc got Unable<hr>";
		`upsmon -c stop 2>&1`;
		`upsd -c stop`;
		my $drv = `ps -A |grep $conf->{'driver'}`;
		if($drv)
		{
			$drv =~ /(\d+) .*/;
			`kill -9 $1`;
			`upsmon -c stop 2>&1`;
		}
		$drv = `ps -A |grep $conf->{'driver'}`;
		if($drv)
		{
			$drv =~ /(\d+) .*/;
			`kill -9 $1`;
			`upsmon -c stop 2>&1`;
		}
	}

}


my $UPSstatus;

my $statusLine = `ps -A|grep upsd`;
#my $statusLine = `upsdrvctl status`;
if($statusLine)
{
	$UPSstatus = "enable";
	&nas_syslog("UPS","naslog_info",19004,"the ups is running!");
}
else
{
	$UPSstatus = "disable";
	&nas_syslog("UPS","naslog_info",19005,"the ups is not running!");
	`upsd -c stop`;

}

if($UPSstatus eq "enable")
{
	my $UPSstate = `upsc localhost`;
	print "<p><b>$text{'service_on'}</b></p>";
	print "<table border=0 width=50% class=mode1 cellspacing=0>";
	my @lines = split('\r|\n',$UPSstate);
	for(my $i=0; $i<@lines ;$i++)
	{
		next if($lines[$i] =~ /^host/);
		next if($lines[$i] =~ /^MODEL/);
		next if($lines[$i] =~ /^SERIAL/);
		$lines[$i] =~ /(.+):(.+)/;
		if($i%2 == 0)
		{
			print "<tr class=mode1><td><b>$1</b></td><td>$2</td></tr>";
		}
		else
		{
			print "<tr class=mode2><td><b>$1</b></td><td>$2</td></tr>";
		}
	}
	print "</table>";
}
else
{
	print "<p><b><i>$text{'service_off'}</i></b></p>";
}

#print $config{'driverlist'};

my $content = &read_ups_list();

#foreach $i (keys %$content)
#{
#	print "<hr>$i--->";
#	foreach $j (@{$content->{$i}})
#	{
#		print " #$j# ";
#	}
#}
&ReadParse();

#if($UPSstatus eq "enable")
#{
	print "<form name=ups method=post>";
#}
#else
#{
#	print "<form name=ups method=post onsubmit='document.ups.stop.disabled=true'>";
#}

print "<table class=mode1 border=0 width=75%>";

print "<tr><td width=30%><b>$text{'manufacturer'}</b></td>";
print "<td>";


print "<select name=company size=1 onChange='document.ups.action=\"index.cgi\";document.ups.submit()'>";

foreach my $i (keys %$content)
{
	if($in{'company'} eq $i)
	{
		print "<option value='$i' selected>$i</option>";
	}
	else
	{
		print "<option value='$i'>$i</option>";
	}
}
print "</select>";


print "</td>";
print "</tr>";

print "<tr><td><b>$text{'model'}</b></td>";
print "<td><select name=model size=1>";

foreach my $i (keys %$content)
{
	if(!$in{'company'})
	{
		foreach my $j (@{$content->{$i}})
		{
			$j =~ /(.+)=(.+)/;
			print "<option value='$1'>$1</option>";
		}
	}
	else
	{
		if($in{'company'} eq $i)
		{
		foreach my $j (@{$content->{$i}})
		{
			$j =~ /(.+)=(.+)/;
			print "<option value='$1'>$1</option>";
		}
		}
		else
		{
			next;
		}
	}
}

print "</select></td>";
print "</tr>";

print "<tr><td><b>$text{'port'}</b></td>";
print "<td><select name=port size=1>";
print "<option value=ttyS0>Com1</option>";
print "<option value=ttyS1>Com2</option>";
print "</select></td>";
print "</tr>";

print "</table>";


print "<p>";
print "<table class=mode1 border=0 width=45% cellspacing=0>";
print "<tr><td>";
print "<input type=submit name=start class=button value='$text{'start_service'}' onclick='document.ups.action=\"upsset.cgi\"'>";
print "</td><td>";
print "<input type=submit name=stop class=button value='$text{'stop_service'}' onclick='document.ups.action=\"upsclear.cgi\"'>";
print "</td></tr></table>";
print "</p>";
print "<input type=hidden name=upsstatus value=$UPSstatus>";

print "</form>";
&footer("/",$text{'index'});

